package com.foot.travel.viewholder;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.foot.travel.MyImageLoadingListener;
import com.foot.travel.R;
import com.foot.travel.activity.MyActivity;
import com.foot.travel.activity.ShowImageActivity;
import com.foot.travel.adapter.ImageAdapter;
import com.koalac.ironman.cache.SysAppConfig;
import com.koalac.ironman.common.adapter.ViewHolderModel;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.DeleteGood;
import com.koalac.ironman.net.pojo.request.GalleryClickGood;
import com.koalac.ironman.net.pojo.response.GalleryItem;
import com.koalac.ironman.net.pojo.response.GalleryPicture;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.net.pojo.response.User;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.util.TimeUtil;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.koalac.ironman.widget.LifeqImageView;
import com.koalac.ironman.widget.NoScrollGridView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GalleryItemViewHolder extends ViewHolderModel<GalleryItem> {

    ImageView singlePhotoView;
    NoScrollGridView imageListView;
    NoScrollGridView fourImageListView;
    LifeqImageView headPhoto;
    TextView nickname;
    TextView mood;
    TextView date;
    TextView likeCount;
    TextView likeTag;

    public static final int NO_PIC = 0;
    public static final int SINGLE_PIC = 1;
    public static final int FOUR_PIC = 4;

    public GalleryItemViewHolder(Context context, List<GalleryItem> list) {
	super(context, list);
    }

    @Override
    public void initViewHoler(int position, View convertView) {
	singlePhotoView = (ImageView) convertView
		.findViewById(R.id.single_photo);
	imageListView = (NoScrollGridView) convertView
		.findViewById(R.id.image_list);
	fourImageListView = (NoScrollGridView) convertView
		.findViewById(R.id.four_image_list);
	headPhoto = (LifeqImageView) convertView.findViewById(R.id.head_photo);
	nickname = (TextView) convertView.findViewById(R.id.nickname);
	mood = (TextView) convertView.findViewById(R.id.mood);
	date = (TextView) convertView.findViewById(R.id.date);
	likeCount = (TextView) convertView.findViewById(R.id.like_count);
	likeTag = (TextView) convertView.findViewById(R.id.like_tag);
    }

    @Override
    public void setViewHolerValues(int position, View convertView) {
	final GalleryItem galleryItem = list.get(position);
	final List<GalleryPicture> galleryPictureList = galleryItem
		.getGalleryPictureList();
	int pictureSize = getPictureListSize(galleryPictureList);
	 int  itemSize;
	switch (pictureSize) {
	case NO_PIC:
	    singlePhotoView.setVisibility(View.GONE);
	    imageListView.setVisibility(View.GONE);
	    fourImageListView.setVisibility(View.GONE);
	    break;
	case SINGLE_PIC:
	    singlePhotoView.setVisibility(View.VISIBLE);
	    imageListView.setVisibility(View.GONE);
	    fourImageListView.setVisibility(View.GONE);
		Glide
   	    .with(context)
   	    .load(galleryPictureList.get(0).getPicurl())
   	    .placeholder(R.drawable.ic_empty)
   	    .crossFade()
   	    .into(singlePhotoView);
	    singlePhotoView.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
		    Intent intent = new Intent(context, ShowImageActivity.class);
		    ArrayList<String> imageUrls = new ArrayList<String>();
		    for (GalleryPicture galleryPicture : galleryPictureList) {
			imageUrls.add(galleryPicture.getPicurl());
		    }
		    intent.putExtra(ShowImageActivity.IMAGE_URLS_KEY, imageUrls);
		    intent.putExtra(ShowImageActivity.IMAGE_URL_INDEX, 0);
		    context.startActivity(intent);
		    // Intent intent = new Intent(context,
		    // PhotoPreviewActivity.class);
		    // ArrayList<PhotoInfo> photoInfos = new
		    // ArrayList<PhotoInfo>();
		    // for (GalleryPicture galleryPicture : galleryPictureList)
		    // {
		    // PhotoInfo photoInfo = new PhotoInfo();
		    // photoInfo.setPhotoUrl(galleryPicture.getPicurl());
		    // photoInfos.add(photoInfo);
		    // }
		    // intent.putExtra(PhotoPreviewActivity.PHOTO_LIST,
		    // photoInfos);
		    // context.startActivity(intent);
		}
	    });
	    break;
	case FOUR_PIC:
	    itemSize = (ScreenInfoUtil.getScreenWidth(context) - ScreenInfoUtil.dp2px(context, 125)) / 2;
	    initMultiImage(fourImageListView, galleryPictureList,itemSize);
	    break;
	default:
	    itemSize = (ScreenInfoUtil.getScreenWidth(context) - ScreenInfoUtil.dp2px(context, 80)) / 3;
	    initMultiImage(imageListView, galleryPictureList,itemSize);
	    break;
	}
	headPhoto.loadFile(galleryItem.getHeadImgUrl(), ImageType.SourceImage,
		null, null);
	nickname.setText(galleryItem.getAuthorName());
	mood.setText(galleryItem.getMessage());
	if (position == 0 || position == 1) {
	    Log.i("galleryItem",
		    "" + position + " " + galleryItem.getDateline());
	}
	date.setText(TimeUtil.getTimeDescription(galleryItem.getDateline()));
	likeCount.setText(galleryItem.getGoodNumber() + "人赞");
	if (galleryItem.getGood_flag() == GalleryItem.LIKED) {
	    likeTag.setText("取消");
	} else {
	    likeTag.setText("赞");
	}

	likeTag.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		if (galleryItem.getGood_flag() == GalleryItem.LIKED) {
		    cancelLike(galleryItem);
		} else {
		    like(galleryItem);
		}
	    }
	});
	headPhoto.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(context, MyActivity.class);
		User user = SysAppConfig.getInstance().getUser(context);
		String uid = user.getId();
		if (uid.equals(galleryItem.getUserId())) {

		} else {
		    intent.putExtra(MyActivity.UID_KEY, galleryItem.getUserId());
		    intent.putExtra(MyActivity.NAV_TITLE,
			    galleryItem.getAuthorName() + "的主页");
		    context.startActivity(intent);
		}
	    }
	});
    }

    private void like(final GalleryItem galleryItem) {

	GalleryClickGood clickGood = new GalleryClickGood();
	clickGood.setId(galleryItem.getId());
	MyRequest.request(context, clickGood, new ResponseCallback() {

	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
		if (result) {
		    galleryItem.setGoodNumber(galleryItem.getGoodNumber() + 1);
		    likeCount.setText(galleryItem.getGoodNumber() + "人赞");
		    galleryItem.setGood_flag(GalleryItem.LIKED);
		    likeTag.setText("取消");
		}
	    }
	});
    }

    private void cancelLike(final GalleryItem galleryItem) {
	DeleteGood deleteGood = new DeleteGood();
	deleteGood.setId(galleryItem.getId());
	MyRequest.request(context, deleteGood, new ResponseCallback() {

	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
		if (result) {
		    galleryItem.setGoodNumber(galleryItem.getGoodNumber() - 1);
		    likeCount.setText(galleryItem.getGoodNumber() + "人赞");
		    galleryItem.setGood_flag(GalleryItem.UNLIKE);
		    likeTag.setText("赞");
		}
	    }
	});
    }

    private void initMultiImage(NoScrollGridView listView,
	    final List<GalleryPicture> galleryPictureList,int itemSize) {
	singlePhotoView.setVisibility(View.GONE);
	imageListView.setVisibility(View.GONE);
	fourImageListView.setVisibility(View.GONE);
	listView.setVisibility(View.VISIBLE);
	ImageAdapter imageAdapter = new ImageAdapter(context,
		galleryPictureList,itemSize);
	listView.setAdapter(imageAdapter);
	listView.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> parent, View view,
		    int position, long id) {
		Intent intent = new Intent(context, ShowImageActivity.class);
		ArrayList<String> imageUrls = new ArrayList<String>();
		for (GalleryPicture galleryPicture : galleryPictureList) {
		    imageUrls.add(galleryPicture.getPicurl());
		}
		intent.putExtra(ShowImageActivity.IMAGE_URLS_KEY, imageUrls);
		intent.putExtra(ShowImageActivity.IMAGE_URL_INDEX, position);
		context.startActivity(intent);
	    }
	});
    }

    private int getPictureListSize(List<GalleryPicture> galleryPictureList) {
	int count = 0;
	for (GalleryPicture galleryPicture : galleryPictureList) {
	    if (!StringUtils.isEmpty(galleryPicture.getPicurl())) {
		count++;
	    }
	}
	return count;
    }

}
